package learn.contorlller;

import learn.bean.LoginUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;

/**
 * @Description:
 * @Author yxc
 * @Date 2022/1/8
 */
@Controller
public class TemplateIndexController {

    /**
     * 登录页
     *
     * @return
     */
    @GetMapping(value = {"/", "login"})
    public String loginPage() {
        return "login";
    }

    /**
     * 防止前端页面重新提交
     *
     * @return
     */
    @PostMapping("/login")
    public String main(LoginUser user, HttpSession session, Model model) {
        if (!StringUtils.isEmpty(user.getUserName()) && !StringUtils.isEmpty(user.getPassword())) {
            session.setAttribute("loginUser", user);
            return "redict:main.html";
        } else {
            model.addAttribute("msg", "账号密码错误");
            return "login";
        }

    }

    @GetMapping("/main.html")
    public String mainPage(HttpSession session) {

        // 判断是否登陆的状态
        Object object = session.getAttribute("loginUser");
        if (object == null) {
            // 跳转回去登录页，给 未登录提示
        } else {
            // 放行
        }
        return "main";
    }

}
